const {Sequelize, DataTypes} = require("sequelize");
const start = require("./utils/connect");

(async()=>{
  const sequelize = await start();
  const CLASS = sequelize.define("CLASS",{
    name:{
      type:DataTypes.STRING,
      allowNull:false
    },
    uuid:{
      type:DataTypes.UUID,
      defaultValue:Sequelize.UUIDV1
    },
    member:{
      type:DataTypes.INTEGER.ZEROFILL
    }
  },{
    freezeTableName:true // 强制使用定义表名
  });

  const Student = sequelize.define("Student",{
    name:{
      type:DataTypes.STRING
    },
    age:{
      type:DataTypes.INTEGER,
      defaultValue:24
    },
    uuid:{
      type:DataTypes.UUID,
      defaultValue:Sequelize.UUIDV4
    }
  },{
    freezeTableName:true // 强制使用定义表名
  });

  CLASS.hasMany(Student);


  await Student.sync({
    alter:true,
  });
  await CLASS.sync({
    alter:true,
  });


  // const student1 = await Student.create({name:"侯东骁"});
  // const student2 = await Student.create({name:"季杰"});
  // const _class = await CLASS.create({name:"环境1班"});

  // await _class.setStudents([student1,student2]);
  
  // await _class.addStudents([student1,student2]);

  // const huanjing1 = await CLASS.findByPk(2);
  // const students = await huanjing1.getStudents();

  // console.log(students.map(el=>el.toJSON()));

  // const class2 = await CLASS.findOne({
  //   where:{
  //     id:1
  //   },
  //   include:Student
  // });

  // console.log(class2.toJSON(),'class2');
  
  const student1 = await Student.findOne({
    where:{
      id:1
    },
    include:CLASS
  });
  console.log(student1.toJSON())

  






})();